'use strict';

const Controller = require('egg').Controller;
const mhtml2html = require('fast-mhtml2html');
const fs = require('fs');
class XinzController extends Controller {
  async index() {
    const { ctx } = this;
    let mhtml = await this.getFileData('./files/abc.mhtml',);
    const html = mhtml2html.convert(mhtml)
      // console.log(html);
      ctx.body = {
        data: html
      }
    // fs.readFile('./files/abc.mhtml', 'utf8',(err, data) => {
    //   if (err) {
    //     console.log(err)
    //   }
    //   const html = mhtml2html.convert(data)
    //   // console.log(html);
    //   ctx.body = {
    //     data: html
    //   }
    // })
  };
  getFileData(filePath,encoding = 'utf8'){
    return new Promise((resolve,reject) => {
      fs.readFile(filePath,encoding,function (err,data) {
        if (err) {
          resolve(err); 
        } else {
          resolve(data);
        }
      })
    })
  }
}


module.exports = XinzController;
